/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.globalsolutionslt.controlador;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.PersistenceContext;
import sv.com.globalsolutionslt.entidades.Direccion;

/**
 *
 * @author karinaMelara
 */
@Stateless
public class DireccionFacade extends AbstractFacade<Direccion> {

    @PersistenceContext(unitName = "BoldeDeTrabajoPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DireccionFacade() {
        super(Direccion.class);
    }

    public List<Direccion> direccionEmpresa(Integer idEmpresa) {
        try {
            Query q = getEntityManager().createQuery("SELECT d FROM Direccion d WHERE d.idEmpresa.idEmpresa = :idEmpresa");
            q.setParameter("idEmpresa", idEmpresa);
            System.out.println("Query ----> " + q);
            System.out.println("Resultado :*******>> " + q.getResultList().toString());

            return q.getResultList();

        } catch (NoResultException ne) {
            System.out.println("Error en DireccionFacade");
            return null;
        } catch (Exception e) {
            throw e;
        }
    }
    
    public Direccion bDirecPorPers(Integer p) {
        Direccion direc=null;
        try {        
        String sql = "SELECT d FROM Direccion d  WHERE d.idPersona.idPersona="+p;
        Query q = em.createQuery(sql);
        direc = (Direccion) q.getSingleResult();
        
        } catch (NoResultException e) {
                System.out.println("Sin resultados");
        }
        return direc;
    }    

}
